package com.sskj.wechat.service.impl;

import java.util.Calendar;
import java.util.Date;

import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sskj.core.annotation.ControllerLogExeTime;
import com.sskj.core.exception.MException;
import com.sskj.core.util.JsonUtil;
import com.sskj.wechat.domain.WeChatUserQueryParam;
import com.sskj.wechat.domain.entity.WeChatUserEntity;
import com.sskj.wechat.repository.WeChatUserRepository;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "/wechat/user", method = { RequestMethod.POST, RequestMethod.GET })
@Transactional
public class WeChatUserServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	WeChatUserRepository weChatUserRepository;

	@RequestMapping(value = "/page")
	@ControllerLogExeTime(description = "查询微信关注用户信息", log = false)
	@ApiOperation(value = "查询微信关注用户信息", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "姓名", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "pageNumber", value = "页号", paramType = "form", dataType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "页的大小", paramType = "form", dataType = "int") })
	public Page<WeChatUserEntity> queryPage(@ApiIgnore WeChatUserQueryParam weChatUserQueryParam,
			@ApiIgnore Pageable pageable) {

		Page<WeChatUserEntity> page = weChatUserRepository.findUserPage(weChatUserQueryParam, pageable);
		return page;
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/isBinding")
	@ApiOperation(value = "查询微信关注用户信息", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "openId", value = "openId", paramType = "form", dataType = "string") })
	public void checkBinDing(@ApiIgnore WeChatUserQueryParam weChatUserQueryParam) {

		WeChatUserEntity weChatUserEntity = weChatUserRepository
				.findBindingUserByOpenId(weChatUserQueryParam.getOpenId());
		if (weChatUserEntity == null) {

			Object[] msg = { "没有绑定用户信息!" };
			throw new MException("custom_msg", msg);
		}
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/binding", method = { RequestMethod.POST })
	@ApiOperation(value = "绑定用户信息", notes = "绑定用户信息", httpMethod = "POST")
	public void binding(@RequestBody WeChatUserEntity weChatUserEntity) throws MException {

		WeChatUserEntity oldWeChatUserEntity = weChatUserRepository.findByOpenId(weChatUserEntity.getOpenId());
		if(null!=oldWeChatUserEntity) {
		oldWeChatUserEntity.setCity(weChatUserEntity.getCity().isEmpty() ? "" : weChatUserEntity.getCity());
		oldWeChatUserEntity.setCountry(weChatUserEntity.getCountry().isEmpty() ? "" : weChatUserEntity.getCountry());
		oldWeChatUserEntity.setGroupId(weChatUserEntity.getGroupId());
		oldWeChatUserEntity
				.setHeadImgUrl(weChatUserEntity.getHeadImgUrl().isEmpty() ? "" : weChatUserEntity.getHeadImgUrl());
		oldWeChatUserEntity.setNickname(weChatUserEntity.getNickname().isEmpty() ? "" : weChatUserEntity.getNickname());
		oldWeChatUserEntity.setOpenId(weChatUserEntity.getOpenId().isEmpty() ? "" : weChatUserEntity.getOpenId());
		oldWeChatUserEntity.setProvince(weChatUserEntity.getProvince().isEmpty() ? "" : weChatUserEntity.getProvince());
		oldWeChatUserEntity.setCertNum(weChatUserEntity.getCertNum().isEmpty() ? "" : weChatUserEntity.getCertNum());
		oldWeChatUserEntity.setTelNum(weChatUserEntity.getTelNum().isEmpty() ? "" : weChatUserEntity.getTelNum());
		oldWeChatUserEntity.setRemark(weChatUserEntity.getRemark() == null ? "" : weChatUserEntity.getRemark());
		oldWeChatUserEntity.setSex(weChatUserEntity.getSex().isEmpty() ? "" : weChatUserEntity.getSex());
		oldWeChatUserEntity.setSexId(weChatUserEntity.getSexId());
		oldWeChatUserEntity.setUnionId(weChatUserEntity.getUnionId());
		oldWeChatUserEntity.setIsBinDing(true);
		this.logger.info("wechatUserEntity : {}", JsonUtil.json(oldWeChatUserEntity));
		weChatUserRepository.save(oldWeChatUserEntity);
		}

	}

	public void save(WxMpUser wxMpUser) {

		WeChatUserEntity oldWeChatUserEntity = weChatUserRepository.findByOpenId(wxMpUser.getOpenId());

		if (oldWeChatUserEntity == null) {
			oldWeChatUserEntity = new WeChatUserEntity();
		}
		oldWeChatUserEntity.setCity(wxMpUser.getCity());
		oldWeChatUserEntity.setCountry(wxMpUser.getCountry());
		oldWeChatUserEntity.setGroupId(wxMpUser.getGroupId());
		oldWeChatUserEntity.setHeadImgUrl(wxMpUser.getHeadImgUrl());
		oldWeChatUserEntity.setLanguage(wxMpUser.getLanguage());
		oldWeChatUserEntity.setNickname(wxMpUser.getNickname());
		oldWeChatUserEntity.setOpenId(wxMpUser.getOpenId());
		oldWeChatUserEntity.setProvince(wxMpUser.getProvince());
		oldWeChatUserEntity.setSubscribeTime(Calendar.getInstance());
		oldWeChatUserEntity.getSubscribeTime().setTime(new Date(wxMpUser.getSubscribeTime() * 1000));
		oldWeChatUserEntity.setRemark(wxMpUser.getRemark());
		oldWeChatUserEntity.setSex(wxMpUser.getSex());
		oldWeChatUserEntity.setSexId(wxMpUser.getSexId());
		oldWeChatUserEntity.setUnionId(wxMpUser.getUnionId());
		oldWeChatUserEntity.setSubscribe(wxMpUser.getSubscribe());
		weChatUserRepository.save(oldWeChatUserEntity);
	}

}
